package strings;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;

import net.sf.json.JSONObject;

/**
 *     小程序码
 * @author caoxiabo
 *
 */
public class WXTest {

    public static void main(String[] args) {
        //wxcode1();
        //wxcode2();
        //wxcode3();
    }

    private static void wxcode3() {
        try {
            URL url = new URL("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=24_uLl3Hc-pr-GfOdTE5Im2XccJPykX9dAZHo90r9nvlOej13P-3FmANU53LiMIAmwZ3l0pDsMWPD-Af2yNX_RVdqZuUklTi5l36mEVTGjMeigffHUaTYhq6rrMwo0XLQqd2T-od41GMDvbNYi1GYChAHANFJ");
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setRequestMethod("POST");// 提交模式
            // conn.setConnectTimeout(10000);//连接超时 单位毫秒
            // conn.setReadTimeout(2000);//读取超时 单位毫秒
            // 发送POST请求必须设置如下两行
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setDoInput(true);
            // 获取URLConnection对象对应的输出流
            PrintWriter printWriter = new PrintWriter(httpURLConnection.getOutputStream());
            // 发送请求参数
            JSONObject paramJson = new JSONObject();

            paramJson.put("scene", "a=1234567890");
            paramJson.put("page", "pages/search/search");
            paramJson.put("width", 430);
            paramJson.put("auto_color", true);

//            paramJson.put("path", "pages/search/search");
//            paramJson.put("width", 430);
//            paramJson.put("auto_color", true);

            /**
             * line_color生效
             * paramJson.put("auto_color", false);
             * JSONObject lineColor = new JSONObject();
             * lineColor.put("r", 0);
             * lineColor.put("g", 0);
             * lineColor.put("b", 0);
             * paramJson.put("line_color", lineColor);
             * */

            printWriter.write(paramJson.toString());
            // flush输出流的缓冲
            printWriter.flush();
            //开始获取数据
            BufferedInputStream bis = new BufferedInputStream(httpURLConnection.getInputStream());
            OutputStream os = new FileOutputStream(new File("C:/Users/caoxiabo/Desktop/abc.png"));
            int len;
            byte[] arr = new byte[1024];
            while ((len = bis.read(arr)) != -1) {
                os.write(arr, 0, len);
                os.flush();
            }
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void wxcode2() {
        try {
            URL url = new URL("https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=24_QUQ278muzMZVaSPDwhLia7thErd83HjIoeBl7I1I24OYV0wWiq4x3VMQAK6-BrC_kAnnpHDxE-ymy4hOLLCFMkx_f81vYDIaaTlpeQZYJ-Up-ySBoYIGBzOY6_ACRygcsp2K8DtlcYHSpJTaWVThACAAZX");
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setRequestMethod("POST");// 提交模式
            // conn.setConnectTimeout(10000);//连接超时 单位毫秒
            // conn.setReadTimeout(2000);//读取超时 单位毫秒
            // 发送POST请求必须设置如下两行
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setDoInput(true);
            // 获取URLConnection对象对应的输出流
            PrintWriter printWriter = new PrintWriter(httpURLConnection.getOutputStream());
            // 发送请求参数
            JSONObject paramJson = new JSONObject();

            //paramJson.put("scene", "a=1234567890");
            //paramJson.put("page", "pages/search/search");
            //paramJson.put("width", 430);
            //paramJson.put("auto_color", true);

            paramJson.put("path", "pages/search/search");
            paramJson.put("width", 430);
            paramJson.put("auto_color", false);
            JSONObject lineColor = new JSONObject();
            lineColor.put("r", 0);
            lineColor.put("g", 0);
            lineColor.put("b", 0);
            paramJson.put("line_color", lineColor);

            /**
             * line_color生效
             * paramJson.put("auto_color", false);
             * JSONObject lineColor = new JSONObject();
             * lineColor.put("r", 0);
             * lineColor.put("g", 0);
             * lineColor.put("b", 0);
             * paramJson.put("line_color", lineColor);
             * */

            printWriter.write(paramJson.toString());
            // flush输出流的缓冲
            printWriter.flush();
            //开始获取数据
            BufferedInputStream bis = new BufferedInputStream(httpURLConnection.getInputStream());
            OutputStream os = new FileOutputStream(new File("C:/Users/caoxiabo/Desktop/abc.png"));
            int len;
            byte[] arr = new byte[1024];
            while ((len = bis.read(arr)) != -1) {
                os.write(arr, 0, len);
                os.flush();
            }
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void wxcode1() {
        try {
            URL url = new URL("https://api.weixin.qq.com/wxa/getwxacode?access_token=24_na7Wvx3Ny-KEyoMDf7JrMBouobyFU3JYKWsfuu5dXArWxEh8OtsSxyvtejggd4u9ULEOzE5-8USUGVf6yp1228yA-fZ0f-x_6R2mUjuWaURRDdsb9wPfO6d0DNio2rKGEhGxDV6338YLe9qhQKKjAEABVV");
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setRequestMethod("POST");// 提交模式
            // conn.setConnectTimeout(10000);//连接超时 单位毫秒
            // conn.setReadTimeout(2000);//读取超时 单位毫秒
            // 发送POST请求必须设置如下两行
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setDoInput(true);
            // 获取URLConnection对象对应的输出流
            PrintWriter printWriter = new PrintWriter(httpURLConnection.getOutputStream());
            // 发送请求参数
            JSONObject paramJson = new JSONObject();

            //paramJson.put("scene", "a=1234567890");
            //paramJson.put("page", "pages/search/search");
            //paramJson.put("width", 430);
            //paramJson.put("auto_color", true);

            paramJson.put("path", "pages/search/search");
            paramJson.put("width", 430);
            paramJson.put("auto_color", true);

            /**
             * line_color生效
             * paramJson.put("auto_color", false);
             * JSONObject lineColor = new JSONObject();
             * lineColor.put("r", 0);
             * lineColor.put("g", 0);
             * lineColor.put("b", 0);
             * paramJson.put("line_color", lineColor);
             * */
            printWriter.write(paramJson.toString());
            // flush输出流的缓冲
            printWriter.flush();
            //开始获取数据
            BufferedInputStream bis = new BufferedInputStream(httpURLConnection.getInputStream());
            OutputStream os = new FileOutputStream(new File("C:/Users/caoxiabo/Desktop/abc.png"));
            int len;
            byte[] arr = new byte[1024];
            while ((len = bis.read(arr)) != -1) {
                os.write(arr, 0, len);
                os.flush();
            }
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
